﻿using System.Data;
using System.Data.SqlClient;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.Server;

namespace SyncApp.RemoteProvider.RemoteSyncProviders
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class AppRemoteProviderSS2008 : AppRemoteProviderBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="AppRemoteProviderSS2008"/> class.
        /// </summary>
        protected AppRemoteProviderSS2008()
            : base(ChangeTrackingType.SqlServerChangeTracking)
        {
            //Create a command to retrieve a new anchor value with batching from
            //the server
            var selectNewAnchorCommand = new SqlCommand
            {
                CommandText = "usp_GetNewBatchAnchor",
                CommandType = CommandType.StoredProcedure
            };
            selectNewAnchorCommand.Parameters.Add("@" +
              SyncSession.SyncLastReceivedAnchor, SqlDbType.BigInt, 8);
            selectNewAnchorCommand.Parameters.Add("@" +
              SyncSession.SyncMaxReceivedAnchor, SqlDbType.BigInt, 8);
            selectNewAnchorCommand.Parameters.Add("@" +
              SyncSession.SyncNewReceivedAnchor, SqlDbType.BigInt, 8);
            selectNewAnchorCommand.Parameters.Add("@" +
              SyncSession.SyncBatchSize, SqlDbType.Int, 4);
            selectNewAnchorCommand.Parameters.Add("@" +
              SyncSession.SyncBatchCount, SqlDbType.Int, 4);
            selectNewAnchorCommand.Parameters["@" +
              SyncSession.SyncMaxReceivedAnchor].Direction =
                ParameterDirection.Output;
            selectNewAnchorCommand.Parameters["@" +
              SyncSession.SyncNewReceivedAnchor].Direction =
                ParameterDirection.Output;
            selectNewAnchorCommand.Parameters["@" +
              SyncSession.SyncBatchCount].Direction =
                ParameterDirection.InputOutput;
            // Set to the DbServerSyncProvider
            this.SelectNewAnchorCommand = selectNewAnchorCommand;
            // Set the requested batch size
            this.BatchSize = 100;
        }

        protected override SyncAdapter BidirectionalSyncAdpaterHelper(string table)
        {
            var builder = new SqlSyncAdapterBuilder
                              {
                                  ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking,

                                  Connection = Connection as SqlConnection,
                                  SyncDirection = SyncDirection.Bidirectional,
                                  TableName = table
                              };

            return builder.ToSyncAdapter();
        }

        protected override SyncAdapter DownloadOnlySyncAdpaterHelper(string table)
        {
            var builder = new SqlSyncAdapterBuilder
                              {
                                  ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking,
                                  Connection = Connection as SqlConnection,
                                  SyncDirection = SyncDirection.DownloadOnly,
                                  TableName = table
                              };

            return builder.ToSyncAdapter();
        }

        protected override SyncAdapter UploadOnlySyncAdpaterHelper(string table)
        {
            var builder = new SqlSyncAdapterBuilder
                              {
                                  ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking,
                                  Connection = Connection as SqlConnection,
                                  SyncDirection = SyncDirection.UploadOnly,
                                  TableName = table
                              };

            //Get the schema from the server.
            var cmdSelectSchema = new SqlCommand
                                      {
                                          CommandText =
                                              (string.Format("SELECT * FROM {0}", table)),
                                          Connection = Connection as SqlConnection
                                      };

            var updloadSyncAdpt = builder.ToSyncAdapter();

            updloadSyncAdpt.SelectIncrementalInsertsCommand = cmdSelectSchema;

            return updloadSyncAdpt;
        }

        protected override SyncAdapter SnapshotSyncAdpaterHelper(string table)
        {
            var builder = new SqlSyncAdapterBuilder
                              {
                                  ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking,
                                  Connection = Connection as SqlConnection,
                                  SyncDirection = SyncDirection.Snapshot,
                                  TableName = table
                              };

            return builder.ToSyncAdapter();
        }
    }
}